import django_filters
from django_filters import rest_framework as filters
from .models import ScannedFood, UserFoodDiary, UserNutritionGoal, ScanHistory


class ScannedFoodFilter(filters.FilterSet):
    name = django_filters.CharFilter(lookup_expr='icontains')
    brand = django_filters.CharFilter(lookup_expr='icontains')
    barcode = django_filters.CharFilter(lookup_expr='exact')
    min_calories = django_filters.NumberFilter(field_name='nutrition__calories', lookup_expr='gte')
    max_calories = django_filters.NumberFilter(field_name='nutrition__calories', lookup_expr='lte')

    class Meta:
        model = ScannedFood
        fields = ['name', 'brand', 'barcode', 'serving_unit']


class UserFoodDiaryFilter(filters.FilterSet):
    date = django_filters.DateFilter(field_name='consumed_date')
    start_date = django_filters.DateFilter(field_name='consumed_date', lookup_expr='gte')
    end_date = django_filters.DateFilter(field_name='consumed_date', lookup_expr='lte')
    meal_type = django_filters.CharFilter(lookup_expr='exact')

    class Meta:
        model = UserFoodDiary
        fields = ['meal_type', 'food', 'consumed_date']


class UserNutritionGoalFilter(filters.FilterSet):
    period_type = django_filters.CharFilter(lookup_expr='exact')
    is_active = django_filters.BooleanFilter()

    class Meta:
        model = UserNutritionGoal
        fields = ['period_type', 'is_active']


class ScanHistoryFilter(filters.FilterSet):
    status = django_filters.CharFilter(lookup_expr='exact')
    start_date = django_filters.DateFilter(field_name='created_at', lookup_expr='gte')
    end_date = django_filters.DateFilter(field_name='created_at', lookup_expr='lte')

    class Meta:
        model = ScanHistory
        fields = ['status', 'food']